<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "tag".
 *
 * @property int $id
 * @property string $name
 * @property int $frequency
 */
class Tag extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tag';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['frequency'], 'integer'],
            [['name'], 'string', 'max' => 128],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'frequency' => 'Frequency',
        ];
    }

    public static function string2array($tags)
    {
        return explode(',', $tags);
    }

    public static function array2string($tags)
    {
        return implode(",", $tags);
    }

    public static function addTags($tags)
    {
        if (empty($tags)) return ;
        foreach ($tags as $v) {
            $tag = Tag::find()->where(['name' => $v])->one();
            $tagCount = Tag::find()->where(['name' => $v])->count();
            if (!$tagCount) {
                $tagModel = new Tag();
                $tagModel->name = $v;
                $tagModel->frequency = 1;
                $tagModel->save();
            } else {
                $tag->frequency += 1;
                $tag->save();
            }
        }
    }

    public static function removeTags($tags)
    {
        if (empty($tags)) return ;
        foreach ($tags as $v) {
            $tag = Tag::find()->where(['name' => $v])->one();
            $tagCount = Tag::find()->where(['name' => $v])->count();
            if ($tagCount) {
                if ($tagCount && $tag->frequency <= 1) {
                    $tag->delete();
                } else {
                    $tag->frequency -= 1;
                    $tag->save();
                }
            }
        }
    }

    public static function updateFrequency($oldTags, $newTags)
    {
        if (!empty($oldTags) || !empty($newTags)) {
            $oldTagsArray = self::string2array($oldTags);
            $newTagsArray = self::string2array($newTags);
            self::addTags(array_values(array_diff($newTagsArray, $oldTagsArray)));
            self::removeTags(array_values(array_diff($oldTagsArray, $newTagsArray)));
        }
    }

    public static function findTagWeights($limit = 20)
    {
        $tag_level_size = 5;

        $models = Tag::find()->orderBy('frequency desc')->all();
        $total = Tag::find()->limit($limit)->count();

        $stepper = ceil($total/$tag_level_size);

        $tags = [];
        $counter = 1;

        if ($total > 0) {
            foreach ($models as $model) {
                $weight = ceil($counter/$stepper) + 1;
                $tags[$model->name] = $weight;
                $counter++;
            }
        }
        ksort($tags);
        return $tags;
    }
}
